#jinja2: trim_blocks: False
server {
    listen       80;
    server_name  localhost;
    root         /usr/local/openresty/nginx/html;
    {%- if playbook_name == 'standalone' %}
    set $backend_server "http://{{ '127.0.0.1' if is_host_deploy else 'jingwei-backend' }}:8080";
    {%- elif playbook_name == 'serverclient' and server_vip is defined  %}
    set $backend_server "http://{{ server_vip }}:8080";
    {%- elif playbook_name == 'serverclient' %}
    set $backend_server "http://{{ hostvars[groups['jw_server_node'][0]].ansible_host }}:8080";
    {%- endif %}
    # 处理跨域的通用配置
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,areacode,sceneid,scenemodu,scenetype';
    add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';

    if ($request_method = 'OPTIONS') {
        return 200;
    }

    {%- if playbook_name == 'standalone' or playbook_name == 'serverclient' and 'jw_server_node' in group_names %}
    location / {
        content_by_lua_file /usr/local/openresty/bin/lsbkd.lua;
    }
    {%- elif playbook_name == 'serverclient' and 'jw_client_node' in group_names %}
    location / {
        proxy_pass "http://{{ hostvars[groups['jw_server_node'][0]].ansible_host }}:{{ expose_frontend_http_port }}";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    {%- endif %}

    {%- if playbook_name == 'standalone' or playbook_name == 'serverclient' and 'jw_server_node' in group_names %}
    location @static_files {
        root /usr/local/openresty/nginx/html;
        try_files $uri $uri/ /index.html;
        index index.html index.htm;
    }
    {%- endif %}

    location /api/check_container {
        content_by_lua_file /usr/local/openresty/bin/lsbkd.lua;
    }

    location /prod-api {
        set $backend $backend_server;
        set $proxy_pass $backend;
        content_by_lua_file /usr/local/openresty/bin/lsbkd.lua;
    }

    location @backend_prod_api {
        proxy_pass $proxy_pass;
        rewrite ^/prod-api(.*)$ /api$1 break;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header Host $host;
        client_max_body_size  4096M;
    }

    location /gis_server_anbao {
        set $backend $backend_server;
        set $proxy_pass $backend;
        content_by_lua_file /usr/local/openresty/bin/lsbkd.lua;
    }

    location @backend_gis_server_anbao {
        proxy_pass $proxy_pass;
        rewrite ^/gis_server_anbao(.*)$ /api$1 break;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header Host $host;
        client_max_body_size  4096M;
    }

    {%- if playbook_name == 'standalone' or playbook_name == 'serverclient' and 'jw_server_node' in group_names %}
    location /map {
        alias /opt/anbao/map;
        allow all;
        autoindex on;
    }
    {%- else %}
    location /map {
        proxy_pass http://{{ hostvars[groups['jw_server_node'][0]].ansible_host }}:{{ expose_frontend_http_port }};
        rewrite ^/map/(.*)$ /map/$1 break;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header Host $host;
        client_max_body_size  4096M;
    }
    {%- endif %}

    {%- if playbook_name == 'standalone' or playbook_name == 'serverclient' and 'jw_client_node' in group_names %}
    location = /qgz {
        alias /opt/anbao/qgz;
        allow all;
        autoindex on;
    }
    location /qgz/ {
        alias /opt/anbao/qgz/;
        charset utf-8;
        allow all;
        autoindex on;
    }
    {%- endif %}

    # 安保大屏
    location /SCMAP {
        alias  /opt/anbao/project/web/SCMAP;
        index  index.html index.htm;
    }
    {%- if playbook_name == 'standalone' %}
    location /locqtapi  {
        proxy_pass http://{{ 'localhost' if is_host_deploy else 'jingwei-jingweiprinter' }}:5890/api;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header Host $host;
        client_max_body_size  1024M;
    }
    {%- elif playbook_name == 'serverclient' and 'jw_client_node' in group_names %}
    location /locqtapi  {
        proxy_pass http://localhost:5890/api;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header Host $host;
        client_max_body_size  1024M;
    }
    {%- endif %}
    # 文件上传
    {%- if playbook_name == 'standalone' or playbook_name == 'serverclient' and 'jw_server_node' in group_names %}
    location /common {
        alias  /opt/anbao/project/common;
        index  index.html index.htm;
    }
    {%- else  %}
    location /common {
        #alias  /opt/anbao/project/common;
        #index  index.html index.htm;
        proxy_pass http://{{ xserver_host }}:{{ expose_frontend_http_port }};
        rewrite ^/common/(.*)$ /common/$1 break;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header Host $host;
        client_max_body_size  4096M;
    }
    {%- endif %}

    # extent
    location /video {
        proxy_pass http://localhost:8085;
        rewrite ^/video/(.*)$ /$1 break;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header Host $host;
        client_max_body_size  4096M;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
